package base;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.example.costs.MainActivity;
import com.example.costs.ParentLV;
import com.example.costs.R;
import com.example.costs.TrListViewArrayAdapter;
import com.example.costs.TrListViewExpAdapter;

import java.util.ArrayList;

/**
 * Created by Developer on 22.06.13.
 */
public class Transaction {
//    ID пользователя
    private Integer uId;
//    Дата транзакции
    private String date;
//    Сумма транзакции
    private double amount;
//    Категория
    Category ctg;
//    Комментарий
    String descriprion;

    public Transaction(Integer uId, String date, String amount, Integer ctgId, String ctgName, String ctgType, String descriprion) {
        this.uId = uId;
        if(uId == null){
            new NullPointerException("Transaction uId is null");
        }
        this.date = "".equals(date)? (new java.util.Date()).toLocaleString():date;
        this.amount = new Double(amount);
        this.ctg = new Category(uId,ctgId,ctgName,ctgType);
        this.descriprion = descriprion;
        validate();
    }

    public void validate(){
        if(date == null){
            new NullPointerException("Transaction date is null");
        } if(amount == 0){
            new NullPointerException("Transaction amount is 0");
        } if(amount == 0){
            new NullPointerException("Transaction amount is 0");
        }
    }

    public void toBase(){
        MainActivity.db.addTrans(uId, date, amount, ctg.getCtgId(), ctg.getType(), descriprion);
    }


    public Integer getuId() {
        return uId;
    }

    public void setuId(Integer uId) {
        this.uId = uId;
    }

    public String getDate() {
        return this.date;
    }

    public void setDate(String date) {
        this.date = "".equals(date)? (new java.util.Date()).toLocaleString():date;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = new Double(amount);
    }

    @Override
    public String toString(){
        return "uId - "+uId.toString()+"; date - "+date+"; amount - "+amount+"; ctg - "+ctg.toString()+" descriprion - "+descriprion+";";
    }

    public  static void getAllTr(ViewPager viewPager, LayoutInflater activity, String trType){
        ExpandableListView listView = (ExpandableListView) viewPager.findViewById(R.id.listViewOutcome);
        switch (trType.toCharArray()[0]) {
            case 'e':
                MainActivity.trType = "e";
                break;
            case 'i':
                listView = (ExpandableListView) viewPager.findViewById(R.id.listViewIncome);
                MainActivity.trType = "i";
                break;
        }

//        // массив имен атрибутов, из которых будут читаться данные
//        String[] from = {DB.TR_DATE, DB.CTG_NAME, DB.TR_AMOUNT};
//        // массив ID View-компонентов, в которые будут вставлять данные
//        int[] to = { R.id.lDateTr, R.id.lCtgNameTr, R.id.lPrice};
//        ArrayList data = MainActivity.db.fetchAllTr(MainActivity.user.getuId(),MainActivity.trType);
//        TrListViewArrayAdapter adapterTr = new TrListViewArrayAdapter(activity, data,R.layout.list_item_layout, from, to);
//        listView.setAdapter(adapterTr);


        ArrayList<ParentLV> dataPar = MainActivity.db.fetchAllTrParent(MainActivity.user.getuId(), MainActivity.trType);
        listView.setAdapter(new TrListViewExpAdapter(activity,dataPar));
    }
}
